package com.bort.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bort.system.mapper.MenuMapper;
import com.bort.system.model.Menu;
import com.bort.system.model.vo.MenuVo;
import com.bort.system.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Menu> implements MenuService {

    @Resource
    MenuMapper menuMapper;

    public Page<Menu> selectAll(int page,int size,String name) {
        Page<Menu> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(size);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        List<Menu> list = menuMapper.getMenuList(map,pager);
        pager.setRecords(list);
        return pager;
    }

    public void update(Menu menu) {
        if (menu.getParentId() > 0) {
            Menu parent = menuMapper.selectByPrimaryKey(menu.getParentId());
            menu.setLevel((byte) (parent.getLevel() + 1));
        }
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    public void add(Menu menu) {
        menu.setCreateTime(new Date());
        if (menu.getParentId() > 0) {
            Menu parent = menuMapper.selectByPrimaryKey(menu.getParentId());
            menu.setLevel((byte) (parent.getLevel() + 1));
        }
        menuMapper.insertSelective(menu);
    }

    public void delete(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    public List<MenuVo> selectAllMenus() {
        return menuMapper.selectAllMenus();
    }
}
